import controlP5.*;


//
// This is a test of the interactive Modest Maps library for Processing
// the modestmaps.jar in the code folder of this sketch might not be 
// entirely up to date - you have been warned!
//
// this is the only bit that's needed to show a map:
//-------Code by Rohan----
import controlP5.*;
//Adding the ControlP5 library

//Adding variables for powerchart
boolean hidePowerChart = true;
float staticplotX1, staticplotY1, staticplotX2, staticplotY2;
float animator = 0;

//-----------------------


InteractiveMap map;

float windowHeight;
float windowWidth;

int degreeOfClustering = 1; 

// buttons take x,y and width,height:
ZoomButton out = new ZoomButton(5,5,14,14,false);
ZoomButton in = new ZoomButton(22,5,14,14,true);
PanButton up = new PanButton(14,25,14,14,UP);
PanButton down = new PanButton(14,57,14,14,DOWN);
PanButton left = new PanButton(5,41,14,14,LEFT);
PanButton right = new PanButton(22,41,14,14,RIGHT);

// all the buttons in one place, for looping:
Button[] buttons = { 
  in, out, up, down, left, right };

PFont font;
boolean gui = true;

int[] quakesToDraw; // to hold the quakes that have been selected and need to be drawn
boolean updated; // to handle when the user updates the range of quakes to be drawn

myDoubleSlider slider;
float sliderX1, sliderY1;
float sliderX2, sliderY2;

myButton playButton;
float playButtonX1, playButtonY1;
float playButtonX2, playButtonY2;
boolean playOn;
int playCount;
float playRate;

CheckBoxes magSelectBox; //where user can select magnitudes
float magSelectBoxX1, magSelectBoxY1;
float magSelectBoxX2, magSelectBoxY2;
boolean magSelected[];
int quakeMagColors[];

CheckBoxes depthSelectBox;
float depthSelectBoxX1, depthSelectBoxY1;
float depthSelectBoxX2, depthSelectBoxY2;
boolean depthSelected[];
int[] depthColors;
int depthCategories;

CheckBoxes timeSelectBox;
float timeSelectBoxX1, timeSelectBoxY1;
float timeSelectBoxX2, timeSelectBoxY2;

Clock clock;

int[][] magFreqArray;
StackPlot magnitudePlot;
float magnitudePlotX1, magnitudePlotY1;
float magnitudePlotX2, magnitudePlotY2;

int[][] depthFreqArray;
StackPlot depthPlot;
float depthPlotX1, depthPlotY1;
float depthPlotX2, depthPlotY2;

StackPlot timePlot;
int[] timeColors;
int[][] utcFreqArray;
boolean[] timeSelected;
int[][] localTimeFreqArray;
int[][] chiTimeFreqArray;

int plotMode;
int magMode = 0;
int depthMode = 1;
int timeMode = 2;


DataParser dataParser;

Earthquake[][] clusteredQuakes;

ArrayList[][] quakeByLocation;
int[][] quakeIdxByTime;
int latCol = 0;
int lonCol = 1;
int kCol = 2;
int magCol = 3;
int depthCol = 4;

int indicesPerLatDegree = 2;
int indicesPerLonDegree = 2;

double prevZoomLevel;
Location upperLeft;
Location upperRight;
Location lowerLeft;
Location lowerRight;

// added by Guan 
City pop_cities[];  // this is top 100 popular cities
City quake_prone_cities[]; //this is top 10 quake_prone cities
//they can be loaded through two seperate files. 
String typedString;
char typedChars[]  = new char[30];
int typedCount;
boolean cityMode; //added by Jillian
boolean citySelected;
boolean localDraw;
boolean citySel;

boolean bigQuakeMode;
boolean bigQuakeSelected;
int bigQuakeNo;

boolean allEarthquakeMode;
//---------Code by Rohan-----
ControlP5 controlP5;//declaring the controlp5 library variable.
MultiList l;
Controller buttonAll, buttonCities, buttonMag, buttonDepth, buttonTime, buttonHybrid, buttonAerial, buttonRoad;

int[] casualtyArray = new int[10];
int[][] bigQuakeRange = {   {107726, 107785},
                            {184477, 184647},
                            {210886, 211191},
                            {309109, 309231},
                            {364956, 365079},
                            {400763, 401146},
                            {409034, 409357},
                            {455980, 456379},
                            {480034, 480251},
                            {535106, 535443}  };

//----------------------
boolean firstTime = true;

void setup(){
  windowHeight = 1024;
  windowWidth = 1280;
  //clusteredQuakes = new Earthquake[(int) windowWidth/degreeOfClustering][(int) windowHeight/degreeOfClustering];
  size(1280, 1024);
  smooth();
  
  

  // create a new map, optionally specify a provider
//  hybridMap = new InteractiveMap(this, new Microsoft.AerialProvider());
//  aerialMap = new InteractiveMap(this, new Microsoft.AerialProvider());
//  roadMap = new InteractiveMap(this, new Microsoft.AerialProvider());
//  
  map = new InteractiveMap(this, new Microsoft.HybridProvider());
  
  //------Code added by Rohan----
  //endpoints for powerchart
  staticplotX1 = windowWidth - 430;
  staticplotX2 = staticplotX1 + 400;
  staticplotY1 = (windowHeight/4) - 185;
  staticplotY2 = staticplotY1 + 270;
  //-----------------------------------------
  
  //new Microsoft.AerialProvider());//new Microsoft.RoadProvider());
  // others would be "new Microsoft.HybridProvider()" or "new Microsoft.AerialProvider()"
  // the Google ones get blocked after a few hundred tiles
  // the Yahoo ones look terrible because they're not 256px squares :)

  // set the initial location and zoom level to London:
  //map.setCenterZoom(new Location(51.500, -0.126), 11);
  // zoom 0 is the whole world, 19 is street level
  // (try some out, or use getlatlon.com to search for more)

  // set a default font for labels
  font = createFont("Helvetica", 12);

  // enable the mouse wheel, for zooming
  addMouseWheelListener(new java.awt.event.MouseWheelListener() { 
    public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) { 
      mouseWheel(evt.getWheelRotation());
    }
  }
  ); 
  
  dataParser = new DataParser("quake.csv");//"quake.csv");//"fulldata.csv");
  //initClusteredQuakes();
  //quakesToDraw = dataParser.getQuakesToDraw();
  //initQuakes();
  //storeQuakes();
  updated = false;

  //init slider
  sliderX1 = 175;
  sliderY1 = 5;//height-400;
  sliderX2 = width-125;
  sliderY2 = 55;//height-350;
  slider = new myDoubleSlider(sliderX1, sliderY1, sliderX2, sliderY2, 1973, 2010, 10, 1);
  
  playButtonX2 = sliderX1 - 10;
  playButtonX1 = playButtonX2 - 60;
  playButtonY1 = sliderY1 - 5;
  playButtonY2 = sliderY2 + 5;
  playButton = new myButton(playButtonX1, playButtonY1, playButtonX2, playButtonY2, "triangle", "ellipse");
  playOn = false;
  playCount = 0;
  playRate = .5;
  
  int numOfCategories = 7; 
  
  //construct the magnitude selection box
  //constructor CheckBoxes(int numOfBoxes, String boxTitle, String[] boxLabels, float boxAreaX1, float boxAreaY1, float boxAreaX2, float boxAreaY2, int[] boxColors){
  String[] boxLabels = new String[numOfCategories];
  //boxLabels[0] = "0.0\n-\n1.9";
  boxLabels[0] = "0.0\n-\n3.9";
  boxLabels[1] = "4.0\n-\n4.9";
  boxLabels[2] = "5.0\n-\n5.9";
  boxLabels[3] = "6.0\n-\n6.9";
  boxLabels[4] = "7.0\n-\n7.9";
  boxLabels[5] = "8.0\n-\n8.9";
  boxLabels[6] = "9.0\n-\n9.9";
  
  quakeMagColors = new int[numOfCategories];
  quakeMagColors[0] = 0xFFFEE5D9;
  quakeMagColors[1] = 0xFFFCBBA1;
  quakeMagColors[2] = 0xFFFC9272;
  quakeMagColors[3] = 0xFFFB6A4A;
  quakeMagColors[4] = 0xFFEF3B2C;
  quakeMagColors[5] = 0xFFCB181D;
  quakeMagColors[6] = 0xFF99000D;
  //quakeMagColors[7] = 0xFF99000D; 

  magSelected = new boolean[numOfCategories];
  for(int i = 0; i < magSelected.length; i++){
    magSelected[i] = true;
  }
  
  magSelectBoxX2 = width-10;//magSelectBoxX1 + 200;
  magSelectBoxX1 = magSelectBoxX2-200;//3*width/4-50;
  magSelectBoxY1 = 2*height/3-75;//2*height/3-50;
  magSelectBoxY2 = magSelectBoxY1 + 100;
 
  magSelectBox = new CheckBoxes(numOfCategories, "Magnitude", boxLabels, magSelectBoxX1, magSelectBoxY1, magSelectBoxX2, magSelectBoxY2, quakeMagColors);
  
  //this is dummy code to initialize magnitude plot until we get queries for frequencies working
  magFreqArray = dataParser.getMagFreq(); //fillFreqArray();
  depthFreqArray = dataParser.getDepthFreq();
  localTimeFreqArray = dataParser.getLocalTimeFreq();
  utcFreqArray = dataParser.getUTCFreq();
  chiTimeFreqArray = dataParser.getChiTimeFreq();
  
  magnitudePlotY2 = 2*windowHeight/3-40;//magnitudePlotY1 + 200;
  magnitudePlotY1 = magnitudePlotY2-200;//150;
  magnitudePlotX1 = 100; //magnitudePlotX2 - 400;
  magnitudePlotX2 = magnitudePlotX1+400;//width-10;
  magnitudePlot = new StackPlot(magFreqArray, new Date(1, 1973), new Date( 6, 2010 ), magnitudePlotX1, magnitudePlotY1, magnitudePlotX2, magnitudePlotY2, "Quake frequency by magnitude", "Year", 
    "frequency\nof quakes\n per month\nby magnitude", quakeMagColors);
    
  depthCategories = 3;
  depthSelected = new boolean[depthCategories];
  for(int i = 0; i < depthSelected.length; i++){
      depthSelected[i]= true;
  }
  
  boxLabels = new String[depthCategories];
  //boxLabels[0] = "0.0\n-\n1.9";
  boxLabels[0] = "0\n-\n70";
  boxLabels[1] = "70\n-\n300";
  boxLabels[2] = "300\n-\n700";
 
  depthColors = new int[depthCategories];
  depthColors[0] = 0xFFEFEDF5;
  depthColors[1] = 0xFFBCBDDC;
  depthColors[2] = 0xFF756BB1;
  
  depthSelectBoxX2 = width - 10; //depthSelectBoxX1 + 100;
  depthSelectBoxX1 = depthSelectBoxX2 - 100;//magSelectBoxX2 + 10;
  depthSelectBoxY2 = magSelectBoxY1-10;//depthSelectBoxY1 + 100;
  depthSelectBoxY1 = depthSelectBoxY2-100;//magSelectBoxY1;
  depthSelectBox = new CheckBoxes(depthCategories, "Depth\n(km)", boxLabels, depthSelectBoxX1, depthSelectBoxY1, depthSelectBoxX2, depthSelectBoxY2, depthColors); 
  
  depthPlot = new StackPlot(depthFreqArray, new Date(1, 1973), new Date( 6, 2010 ), magnitudePlotX1, magnitudePlotY1, magnitudePlotX2, magnitudePlotY2, "Quake frequency by depth (km)", "Year", 
    "frequency\nof quakes\n per month\nby depth\n(km)", depthColors); 
  
  timeSelected = new boolean[3];
  for(int i = 0; i<timeSelected.length; i++){
    timeSelected[i] = true;
  }
  
  timeColors = new int[3];
  timeColors[0] = 0xFFEDF8B1;
  timeColors[1] = 0xFF7FCDBB;
  timeColors[2] = 0xFF2C7FB8;
  
  boxLabels = new String[3];
  //boxLabels[0] = "0.0\n-\n1.9";
  boxLabels[0] = "0\n-\n8";
  boxLabels[1] = "8\n-\n16";
  boxLabels[2] = "16\n-\n24";
  
  timeSelectBoxX2 = width-10;
  timeSelectBoxX1 = timeSelectBoxX2-100;
  timeSelectBoxY2 = depthSelectBoxY1-25;
  timeSelectBoxY1 = timeSelectBoxY2- 100;
  timeSelectBox = new CheckBoxes( 3, "Quake Time\n(GMT)", boxLabels, timeSelectBoxX1, timeSelectBoxY1, timeSelectBoxX2, timeSelectBoxY2, timeColors);
  
  timePlot = new StackPlot(utcFreqArray, new Date(1, 1973), new Date( 6, 2010 ), magnitudePlotX1, magnitudePlotY1, magnitudePlotX2, magnitudePlotY2, "Quake frequency by time of day (GMT)", "Year", 
    "frequency\nof quakes\n per month\nby time\nof day\nGMT", timeColors); 
  
  plotMode = magMode;
  
  upperLeft = new Location( 90.0, -180.0);
  upperRight = new Location(90.0, 180.0);
  lowerLeft = new Location(-90.0, -180.0);//map.pointLocation(0, height);
  lowerRight = new Location(-90.0, 180.0);//map.pointLocation(width, height);
  prevZoomLevel = map.sc;
  
  
  //HERE WE SET THE VIEW MODE
  cityMode = false;
  localDraw = false;
  bigQuakeMode = false;
  allEarthquakeMode = true;
  
  bigQuakeSelected = false;
  citySelected = false;
  
  loadCities("cities.txt");
  typedCount=0;
  //--------code added by Rohan
  controlP5 = new ControlP5(this); //Creating an instance of the button library variable in setup
  addControlButtons ();
  
  casualtyArray[0] = 0;
  casualtyArray[1] = 11;
  casualtyArray[2] = 166;
  casualtyArray[3] = 75;
  casualtyArray[4] = 0;
  casualtyArray[5] = 227898;
  casualtyArray[6] = 1313;
  casualtyArray[7] = 0;
  casualtyArray[8] = 25;
  casualtyArray[9] = 521;
  
//--------------------------------

citySel = false;
}//end setup  


void draw() {
  background(0);
  
  // draw the map:
 
  map.draw();
  
  //-----------Code added by Rohan--------
  //drawing functions for power chart
  hiddenStaticgraph(staticplotX2,staticplotY1,windowWidth,staticplotY2);
    drawStaticgraph(staticplotX1, staticplotY1, staticplotX2, staticplotY2, hidePowerChart);  
   animator=animator+0.5;
   if(animator>10)
   {
     animator=0;
   }
  //------------------------------------
  
 
  // (that's it! really... everything else is interactions now)
  smooth();
  //println("map.tx = " +map.tx + ", map.ty=" + map.ty + "map.sc = " + map.sc);
  // draw all the buttons and check for mouse-over
  boolean hand = false;
  if (gui) {
    for (int i = 0; i < buttons.length; i++) {
      buttons[i].draw();
      hand = hand || buttons[i].mouseOver();
    }
  }

  // if we're over a button, use the finger pointer
  // otherwise use the cross
  // (I wish Java had the open/closed hand for "move" cursors)
  cursor(hand ? HAND : CROSS);

  if (gui) {
    textFont(font, 12);

    // grab the lat/lon location under the mouse point:
    Location location = map.pointLocation(mouseX, mouseY);

    // draw the mouse location, bottom left:
    fill(0);
    noStroke();
    rect(5, height-5-g.textSize, textWidth("mouse: " + location), g.textSize+textDescent());
    fill(255,255,0);
    textAlign(LEFT, BOTTOM);
    text("mouse: " + location, 5, height-5);

    // grab the center
    location = map.pointLocation(width/2, height/2);

    // draw the center location, bottom right:
    fill(0);
    noStroke();
    float rw = textWidth("map: " + location);
    rect(width-5-rw, height-5-g.textSize, rw, g.textSize+textDescent());
    fill(255,255,0);
    textAlign(RIGHT, BOTTOM);
    text("map: " + location, width-5, height-5);

  }  
  
  //*******  Quake getting and drawing *********
  if( updated ){
    //do something
    updated = false;  //no longer needs to be updated
  }
  
 if( allEarthquakeMode ){
    dataParser.drawQuakes();
 }
 //citySelected = true;
 
 if( cityMode){/// && citySelected){
  //  Location location = new Location(27.42, 85.19);
  //  Point2f p = map.locationPoint(location);
  //  print("I'm in city mode");
    if(firstTime){
      initCities(pop_cities);
      firstTime = false;
    }
    drawCities(pop_cities);
    if(localDraw==true)
     if(typedCount>0)
     //textAlign(RIGHT);
      text(typedString, 70, height - 784 + 20);
      //dataParser.drawQuakesAroundCity(pop_cities[theCity].latitude, pop_cities[theCity].longitude, 100, newZoomLevel);
      if( citySel){
        //println("citySel!");
        //dataParser.drawQuakes();
        //dataParser.drawQuakesAroundCity();
        dataParser.drawQuakes();
      }
      
 }
 if( bigQuakeMode && bigQuakeSelected){  //ADDED BY JILLIAN
   //dataParser.drawQuakes();
   int quakeRow = dataParser.drawQuakesFromRToR(bigQuakeRange[bigQuakeNo-1][0], bigQuakeRange[bigQuakeNo-1][1]); //ONLY DRAWS QUAKES AROUND THESE ROWS
   if( quakeRow == 0 ){
     //haven't gotten to it yet
   } else{
     drawQuakeInfo(bigQuakeNo, dataParser.getHour(quakeRow), dataParser.getDay(quakeRow), dataParser.getMonth(quakeRow), dataParser.getYear(quakeRow), dataParser.getMagnitude(quakeRow), dataParser.getDepth(quakeRow));
   }
 }
  //******* End quake getting and drawing ********
    
  slider.drawSlider();
  playButton.drawButton();
  if( playOn ){
    //println("im playing");
    float slider1Pos = slider.getSlider1Position();
    float slider2Pos = slider.getSlider2Position();
    if(slider1Pos < slider2Pos ){
      slider.setSlider1Position( slider1Pos + playRate);
      int month = slider.getMonth( slider1Pos );
      int year = slider.getYear( slider1Pos);
      clock.updateClock(  new Date( month, year ) );
      dataParser.changeMinDate( month, year );
      dataParser.changeMaxDate( month+1, year);
    } else {//done playing
      slider.setSlider1Position( slider2Pos );
      clock.updateClock(  new Date( slider.getMonth( slider1Pos ), slider.getYear(slider1Pos)) );
      int month = slider.getMonth( slider1Pos );
      int year = slider.getYear( slider1Pos);
      dataParser.changeMinDate( month, year );
      dataParser.changeMaxDate( month, year);
      playOn = false;
    }
  }
  
  magSelectBox.drawCheckBoxes();
  depthSelectBox.drawCheckBoxes();
  timeSelectBox.drawCheckBoxes();
  
  
  
  
  if( plotMode == magMode){
    magnitudePlot.drawPlot();
  } else if( plotMode == depthMode){
    depthPlot.drawPlot();
  } else {//time of day mode
    timePlot.drawPlot();
  }
  
  
  //HERE I DEAL WITH ZOOM AND PAN- change max and min lat and lon
  if( allEarthquakeMode || cityMode || bigQuakeMode){
    boolean locChanged = false;
    double sc = map.sc;
    Location uL = map.pointLocation(0, 0);  
    if( sc > 4.0 && (upperLeft.lat != uL.lat || upperLeft.lon != uL.lon) ){ 
      if (uL.lat > -90 && uL.lat < 90 && uL.lon > -180 && uL.lon < 180){
        println("upper left should become: lat =  " + uL.lat + " lon = " + uL.lon); 
        upperLeft = new Location( uL.lat, uL.lon);
        locChanged = true;
      } 
    }  
  
    Location uR = map.pointLocation(width, 0);
    if( sc > 4.0 && (upperRight.lat != uR.lat || upperRight.lon != uR.lon) ){
      if (uR.lat > -90 && uR.lat < 90 && uR.lon > -180 && uR.lon < 180){
        upperRight = new Location( uR.lat, uR.lon);
        locChanged = true;
      }
    }
  
    Location lL = map.pointLocation(0, height);
    if( sc > 4.0 && (lowerLeft.lat != lL.lat || lowerLeft.lon != lL.lon) ){
      if (lL.lat > -90 && lL.lat < 90 && lL.lon > -180 && lL.lon < 180){
        lowerLeft = new Location( lL.lat, lL.lon);
        locChanged = true;
      }
    }
  
     Location lR = map.pointLocation(width, height);
     if(  sc > 4.0 && (lowerRight.lat != lR.lat || lowerRight.lon != lR.lon) ){
       if (lR.lat > -90 && lR.lat < 90 && lR.lon > -180 && lR.lon < 180){
        lowerRight = new Location( lR.lat, lR.lon);
        locChanged = true;
       }
     }
   
     if(locChanged ){
        dataParser.reSetLatAndLonRange(lowerLeft.lat, upperLeft.lat, upperLeft.lon, lowerRight.lon, map.sc);
        prevZoomLevel = map.sc;
      } 
  }
} 

void keyReleased() {
  if (key == 'g' || key == 'G') {
    gui = !gui;
  }
  else if (key == 's' || key == 'S') {
    save("modest-maps-app.png");
  }
  else if (key == 'z' || key == 'Z') {
    map.sc = pow(2, map.getZoom());
  }
  else if (key == ' ') {
    map.sc = 2.0;
    map.tx = -128;
    map.ty = -128; 
  }
}

void keyPressed(){
    //TO TEST CITY MODE
    //if( key == ')' ){
      //cityMode = true;
      //citySelected = true;
      //Location tokyo = new Location( ( 35+42/60), (139+42/60)); 
      //map.setCenterZoom( tokyo, 7);
      //Point2f p = map.locationPoint( tokyo );
      //fill(0);
      //noStroke();
      //ellipse( p.x, p.y, 10, 10);
      //dataParser.drawQuakesAroundCity((35 + 42/60), (139+42/60) , 100, map.sc); 
      //allEarthquakeMode = false;
    //}
    //TO TEST DIFF PLOT MODE
    //if( key == '(' ){
      //plotMode++;
      //if( plotMode > 2 ){
       // plotMode = 0;
      //}
      //if( plotMode == 0 ){
        //dataParser.setToMagMode();
      //} else if( plotMode == 1){
        //dataParser.setToDepthMode();
      //} else {
       // dataParser.setToTimeMode();
      //}
    //}
    if (key == CODED) {
      if (keyCode == LEFT) {
        map.tx += 5.0/map.sc;
      }
      else if (keyCode == RIGHT) {
        map.tx -= 5.0/map.sc;
      }
      else if (keyCode == UP) {
        map.ty += 5.0/map.sc;
      }
      else if (keyCode == DOWN) {
        map.ty -= 5.0/map.sc;
      }
    }  
    else if (key == '+' || key == '=') {
      map.sc *= 1.05;
    }
    else if (key == '_' || key == '-' && map.sc > 2) {
      map.sc *= 1.0/1.05;
    }//****added by Guan //**********
    else if(key == ENTER){
      if(typedCount>0){
        drawSingleCity(pop_cities);
      }else{map.setCenterZoom(new Location(0, 0), 2);
      //  dataParser.zoomLevel = 2;
        dataParser.reSetLatAndLonRange(-90, 90, -180, 180, 2);
        citySel=false;
      }
    }
    else{
      if ((key == BACKSPACE) || (key == DELETE)) {
        if (typedCount > 0) {
          typedCount--;
        }
        updateTyped(pop_cities);
      } else if (((key >= 'a') && (key <= 'z'))||((key >= 'A') && (key <= 'Z'))) {
     //   println("I'm invisiable");
        invisiable(pop_cities);
        if (typedCount != 30) {  // longest city name  
    	  typedChars[typedCount++] = key;
        }
      }
      updateTyped(pop_cities);
    } 
}

int newZoomLevel = 8;
int theCity;
void drawSingleCity(City[] cities){
  int i;//, newZoomLevel=12;//5;
  String tstring=new String(typedChars, 0, typedCount);
  //print("I'm drawing");
  for(i=0; i<cities.length; i++){
     if(match(cities[i].name, tstring)!=null){ 
     //  print(cities[i].name);
   //  dataParser.drawQuakesAroundCity(cities[i].latitude, cities[i].longitude, 100, newZoomLevel);
     
       map.setCenterZoom(new Location(cities[i].latitude, cities[i].longitude), newZoomLevel);
       //dataParser.initQuakesAroundCity(cities[i].latitude, cities[i].longitude, 100, map.sc);
    //   println((cities[i].latitude-1)+ " " +(cities[i].latitude+1) + " " + (cities[i].longitude-1) + " " + (cities[i].longitude+1) + " " + map.sc);
       //dataParser.reSetLatAndLonRange(cities[i].latitude-1, cities[i].latitude+1, cities[i].longitude-1, cities[i].longitude+1, map.sc);
       citySel = true;
       break;
     }
  }
   //dataParser.drawQuakesAroundCity(cities[i].latitude, cities[i].longitude, 100, newZoomLevel);
   localDraw = true;
   theCity = i;
   //dataParser.drawQuakes();
   cityLat = cities[i].latitude;
   cityLon = cities[i].longitude; 
}
float cityLat;
float cityLon;
void invisiable(City[] cities){
  for(int i=0; i<cities.length; i++)
    cities[i].visiable = false;
 // firstTime = true;
}
void initCities(City[] cities){
  for(int i=0; i<cities.length; i++)
    cities[i].visiable = true;
}
void drawCities(City[] cities){
  fill(0,255,128);
   stroke(255,255,0);
   Location location;
   Point2f p;
   for(int ii=0; ii<cities.length; ii++){  
     if(cities[ii].visiable == true){   
      // println(ii);
       location = new Location(cities[ii].getlat(), 
       cities[ii].getlon());
       p = map.locationPoint(location);
       ellipse(p.x, p.y, 10, 10);
       text(cities[ii].name, p.x, p.y);
      }
   }
}
void loadCities(String fname){
  String[] rows = loadStrings(fname);
  pop_cities = new City[95];
  for(int i=0; i<rows.length; i++)
  {
    String[] columns = split(rows[i], TAB);
   // println(columns[0]);
    pop_cities[i]=new City(columns[0], columns[1],float(columns[2]),float(columns[3]), true);//false);
/*
    pop_cities[i].setcountry(columns[1]);
    pop_cities[i].setlat(float(columns[2]));
    pop_cities[i].setlon(float(columns[3]));
*/
   }
    
}

void updateTyped(City[] cities){
  typedString = new String(typedChars, 0, typedCount);
 // print(typedString+"\n");
   int i;
  if(typedCount!=0){
    //text(typedString, 20, 20);
    for(i=0; i<cities.length; i++){
      String s = cities[i].name;
      String m[] = match(s, typedString);
      if(m!=null){
        cities[i].visiable = true;
      }else{
        cities[i].visiable = false;
      }
    }
  }else{
     for(i=0; i<cities.length; i++)
       cities[i].visiable = false;
  }
}


// see if we're over any buttons, otherwise tell the map to drag
void mouseDragged() {
  //over slider?
   if( mouseY > sliderY1 - 20 && mouseY < sliderY2 + 20 ){//is approx over slider bar
       //see if sliders are exactly over each other, then move in tandem
       if( abs( slider.getSlider2Position() - slider.getSlider1Position()) <= 10 ){
        if( mouseX > slider.getSlider2Position()-slider.getSliderSize()/2 - 50 && mouseX < slider.getSlider2Position() + slider.getSliderSize()/2 +50 ){
          slider.setSlider2Position(mouseX);
          int month2 = slider.getMonth( slider.getSlider2Position());
          int year2 =  slider.getYear( slider.getSlider2Position() );
          dataParser.changeMaxDate( month2 , year2 );
          slider.setSlider1Position(mouseX);
          int month = slider.getMonth( slider.getSlider1Position());
          int year =  slider.getYear( slider.getSlider1Position() );
          dataParser.changeMinDate( month , year );
          magnitudePlot.changeDateRange( new Date( month, year ), new Date (month2, year2));
          depthPlot.changeDateRange( new Date( month, year ), new Date (month2, year2));
          timePlot.changeDateRange( new Date( month, year ), new Date (month2, year2));
        }  
       }
       // see if it is exactly over slider 1
      else if( mouseX > slider.getSlider1Position()-slider.getSliderSize()/2 && mouseX < slider.getSlider1Position() + slider.getSliderSize()/2 ){//is over slider 1 
         slider.setSlider1Position(mouseX); 
         int month = slider.getMonth( slider.getSlider1Position());
         int year =  slider.getYear( slider.getSlider1Position() );
         dataParser.changeMinDate( month , year );
         magnitudePlot.changeDateRange( new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) ));  
         depthPlot.changeDateRange( new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) ));  
         timePlot.changeDateRange( new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) )); 
      }//then see if it is exactly over slider 2
      else if( mouseX > slider.getSlider2Position() - slider.getSliderSize()/2 && mouseX < slider.getSlider2Position() + slider.getSliderSize()/2 ){//is over slider 2
        slider.setSlider2Position(mouseX);
        int month2 = slider.getMonth( slider.getSlider2Position());
        int year2 =  slider.getYear( slider.getSlider2Position() );    
        dataParser.changeMaxDate(month2, year2 ); 
        magnitudePlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));
        depthPlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));
        timePlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));
      }
      //then see if it is approx over slider 1
      else if( mouseX > slider.getSlider1Position()-slider.getSliderSize()/2 - 50 && mouseX < slider.getSlider1Position() + slider.getSliderSize()/2 + 50 ){//is approx over slider 1
          slider.setSlider1Position(mouseX); 
          int month = slider.getMonth( slider.getSlider1Position());
          int year =  slider.getYear( slider.getSlider1Position() );
          dataParser.changeMinDate( month , year );
          magnitudePlot.changeDateRange( new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) ));
          depthPlot.changeDateRange(  new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) ));
          timePlot.changeDateRange(  new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) ));
      }//finally, see if it is approx over slider 2
      else if( mouseX > slider.getSlider2Position() - slider.getSliderSize()/2 - 50 && mouseX < slider.getSlider2Position() + slider.getSliderSize()/2 + 50 ){//is approx over slider 2
        slider.setSlider2Position(mouseX);     
        int month2 = slider.getMonth( slider.getSlider2Position());
        int year2 =  slider.getYear( slider.getSlider2Position() );    
        dataParser.changeMaxDate( month2, year2 ); 
        magnitudePlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));
        depthPlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));
        timePlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));
      }
  }//end over slider
  else{ //over map
    boolean hand = false;
    if (gui) {
      for (int i = 0; i < buttons.length; i++) {
        hand = hand || buttons[i].mouseOver();
        if (hand) break;
      }
    }
    if (!hand) {
      map.mouseDragged(); 
    }
  }
  
}

// zoom in or out:
void mouseWheel(int delta) {
  if (delta > 0) {
    map.sc *= 1.05;
  }
  else if (delta < 0) {
    map.sc *= 1.0/1.05; 
  }
}

// see if we're over any buttons, and respond accordingly:
void mouseClicked() {
  if (in.mouseOver()) {
    map.zoomIn();
  }
  else if (out.mouseOver()) {
    map.zoomOut();
  }
  else if (up.mouseOver()) {
    map.panUp();
  }
  else if (down.mouseOver()) {
    map.panDown();
  }
  else if (left.mouseOver()) {
    map.panLeft();
  }
  else if (right.mouseOver()) {
    map.panRight();
  }
}


void mousePressed(){
  //is over play button?
  if( mouseX > playButtonX1 && mouseX < playButtonX2 && mouseY > playButtonY1 && mouseY < playButtonY2){
    if( !playOn ){//if not currently playing, change to playing
      playButton.changeText("pause");
      playOn = true;
      float slider1Pos = slider.getSlider1Position();
      clock = new Clock( new Date( slider.getMonth( slider1Pos ), slider.getYear( slider1Pos ) ) );
    } else { // was playing and now need to stop
      playButton.changeText("triangle");
      playOn = false;
    }
  }
  //is over slider?
  else if( mouseX > sliderX1 && mouseX < sliderX2 && mouseY > sliderY1 && mouseY < sliderY2 ){//is over slider
    float slider1Position = slider.getSlider1Position();
    float slider2Position = slider.getSlider2Position();
    
    if( mouseX < slider1Position ){      
      slider.setSlider1Position(mouseX); 
      int month = slider.getMonth( slider.getSlider1Position());
      int year =  slider.getYear( slider.getSlider1Position() );
      dataParser.changeMinDate( month , year );
      magnitudePlot.changeDateRange( new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) ));
      depthPlot.changeDateRange( new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) ));
      timePlot.changeDateRange( new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) ));
     }
    else if( mouseX > slider2Position ){
      slider.setSlider2Position(mouseX);     
      int month2 = slider.getMonth( slider.getSlider2Position());
      int year2 =  slider.getYear( slider.getSlider2Position() );    
      dataParser.changeMaxDate( month2 , year2 ); 
      magnitudePlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));
      depthPlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));
      timePlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));
    }
    else if( mouseX - slider1Position < slider2Position - mouseX ){ //if closer to slider 1
      slider.setSlider1Position(mouseX); 
      int month = slider.getMonth( slider.getSlider1Position());
      int year =  slider.getYear( slider.getSlider1Position() );
      dataParser.changeMinDate( month , year );
      magnitudePlot.changeDateRange( new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) ));
      depthPlot.changeDateRange( new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) ));
      timePlot.changeDateRange( new Date( month, year ), new Date (slider.getMonth( slider.getSlider2Position() ), slider.getYear( slider.getSlider2Position() ) ));
    }
    else {
      slider.setSlider2Position(mouseX); 
      int month2 = slider.getMonth( slider.getSlider2Position());
      int year2 =  slider.getYear( slider.getSlider2Position() );    
      dataParser.changeMaxDate( month2 , year2 ); 
      magnitudePlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));
      depthPlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));  
      timePlot.changeDateRange( new Date( slider.getMonth( slider.getSlider1Position() ), slider.getYear( slider.getSlider1Position() ) ), new Date (month2, year2));  
  }
  }//end over slider
  
  if( plotMode == magMode){  
     magnitudePlot.mousePressedCheck(mouseX, mouseY);
  } else if( plotMode == depthMode){
     depthPlot.mousePressedCheck(mouseX, mouseY);
  } else{
     timePlot.mousePressedCheck(mouseX, mouseY);
  }
  //over magSelectBox
  
  //magSelectBox
  boolean[] returned = magSelectBox.isChecked(mouseX, mouseY);
  //boolean[] temp = magSelected;
  //magSelected = returned;
  //println(" checking ");
  for(int i = 0; i< magSelected.length; i++){
    //println( "i " + i + " magSelected[i] = " + magSelected[i] + " returned[i] = " + returned[i]);
    if( magSelected[i] != returned[i] ){//new magnitude values
      //determine what is different
      //println("new value " + i);
      if( magSelected[i] && !returned[i] ){ //remove a category
          //println("remove " + i);
          magnitudePlot.removeCategory(i);
          dataParser.resetMagSel(i);
      } else if( !magSelected[i] && returned[i] ){ //sanity check
            magnitudePlot.addCategory(i);
            dataParser.resetMagSel(i);
      }
    }//end sth is diff
  }//end for loop
  //println( "after returned ");
  for(int i = 0; i< magSelected.length; i++){
    magSelected[i] = returned[i];
    //println( "i " + i + " magSelected[i] = " + magSelected[i] + " returned[i] = " + returned[i]);
  }
  
  //depthSelectBox
  returned = depthSelectBox.isChecked(mouseX, mouseY);
  //println(" checking ");
  for(int i = 0; i< depthSelected.length; i++){
    if( depthSelected[i] != returned[i] ){//new magnitude values
      //determine what is different
      ///println("new value " + i);
      if( depthSelected[i] && !returned[i] ){ //remove a category
          depthPlot.removeCategory(i);
          dataParser.resetDepthSel(i);
      } else if( !depthSelected[i] && returned[i] ){ //sanity check
            depthPlot.addCategory(i);
            dataParser.resetDepthSel(i);
      }
    }//end sth is diff
  }//end for loop

  for(int i = 0; i< depthSelected.length; i++){
    depthSelected[i] = returned[i];
  }
  
  //time select box
   returned = timeSelectBox.isChecked(mouseX, mouseY);
  for(int i = 0; i< timeSelected.length; i++){
    if( timeSelected[i] != returned[i] ){//new magnitude values
      //determine what is different
      //println("new value " + i);
      if( timeSelected[i] && !returned[i] ){ //remove a category
          timePlot.removeCategory(i);
          dataParser.resetTimeSel(i);
      } else if( !timeSelected[i] && returned[i] ){ //sanity check
            timePlot.addCategory(i);
            dataParser.resetTimeSel(i);
      }
    }//end sth is diff
  }//end for loop

  for(int i = 0; i< timeSelected.length; i++){
    timeSelected[i] = returned[i];
  }
  
  //------------Code added by Rohan-----
  //Mouse related code for powerchart
  
   if(mouseX > staticplotX2 && mouseX < windowWidth && mouseY > staticplotY1 && mouseY < staticplotY2)
  {
    if(hidePowerChart)
    {
    hidePowerChart = false;
    println("maximize");
    }
    else
    {
    hidePowerChart = true;
    println("minimize");
    }
  }
  //---------------
  
}//end mousePressed
//----Code added by Rohan---------
void addControlButtons() {
  
  controlP5.setColorBackground(color(50,125));
  controlP5.setColorActive(color(0));
  buttonHybrid = controlP5.addButton("Hybrid Map", 301, width - 255, int(magSelectBoxY2), 80, 15);
  buttonAerial = controlP5.addButton("Aerial Map", 302, width - 170, int(magSelectBoxY2), 80, 15);
  buttonRoad = controlP5.addButton("Road Map", 302, width - 85, int(magSelectBoxY2), 80, 15);
  
  buttonHybrid.setId(301);
  buttonAerial.setId(302);
  buttonRoad.setId(303);
  
  
  buttonAll = controlP5.addButton("All", 201, 7, height - 824, 110, 15);
  
  l = controlP5.addMultiList("mylist", 7, height - 824 + 15, 110, 15);
   MultiListButton buttonMajor;

  buttonMajor = l.add("Major Quakes",202);
  

   
  // add items to a sublist of button "level1"
 
  buttonMajor.add("A",1).setLabel("Macquarie Island (1989)");
   
  buttonMajor.add("B",2).setLabel("Kuril Island (1994)");
  
  buttonMajor.add("C",3).setLabel("Indonesia (1996)");

  buttonMajor.add("D",4).setLabel("Southern Peru (2001)");
 
  buttonMajor.add("E",5).setLabel("Hokkaido (2003)");

  buttonMajor.add("F",6).setLabel("Northern Sumatra (2004)");
 
  buttonMajor.add("G",7).setLabel("Northern Sumatra (2005)");
 
  buttonMajor.add("H",8).setLabel("Kuril Island (2006)");

  buttonMajor.add("I",9).setLabel("Southern Sumatra (2007)");
 
  buttonMajor.add("J",10).setLabel("Offshore Maule (2010)");
 
  
  buttonCities = controlP5.addButton("Cities", 203, 7, height - 824 + 30, 110, 15);
  buttonAll.setId(201);
  buttonMajor.setId(202);
  buttonCities.setId(203);
  
  buttonMag = controlP5.addButton("Magnitude Plot", 101, 100, int(magnitudePlotY2)+40, 80, 15);
  buttonDepth = controlP5.addButton("Depth Plot", 102, 180, int(magnitudePlotY2)+40, 80, 15);
  buttonTime = controlP5.addButton("Time Plot", 103, 260, int(magnitudePlotY2)+40, 80, 15);
  
  buttonMag.setId(101);
  buttonDepth.setId(102);
  buttonTime.setId(103);
  
  
  toggleButton(buttonAll, true);
  toggleButton(buttonMajor, false);
  toggleButton(buttonCities, false);
      

  toggleButton(buttonHybrid, true);
  toggleButton(buttonAerial, false);
  toggleButton(buttonRoad, false);
  
  toggleButton(buttonMag, true);
  toggleButton(buttonDepth, false);
  toggleButton(buttonTime, false);
  
}

void toggleButton(Controller theController, boolean buttonEnabled) {
  if( buttonEnabled ) {
    theController.setColorBackground( color(0) );
    theController.setColorLabel( color(255) ); 
    theController.setColorActive( color(0) );
  }
  else {
    theController.setColorBackground(color(50,125));
    theController.setColorLabel( color(255) ); 
    theController.setColorActive( color(0,125) );
  }
}


void controlEvent(ControlEvent theEvent) {
  //println("got click from " + theEvent.controller().name());
  
  if(theEvent.value()>0 && theEvent.value()<11 || theEvent.value()==202)
  {
    bigQuakeMode = true;
    allEarthquakeMode = false;
    cityMode = false;
    bigQuakeSelected = true;
    bigQuakeNo = int(theEvent.value());
    //ADDED BY JILLIAN
    dataParser.startBigQuakeAnimation();
    if(bigQuakeNo <= 10 && bigQuakeNo >= 1.0){
      dataParser.drawQuakesFromRToR(bigQuakeRange[bigQuakeNo-1][0], bigQuakeRange[bigQuakeNo-1][1]);//107726, 107785); //HERE YOU PASS THE FIRST ROW TO PRINT TO THE LAST ROW TO PRINT-- THIS FUNCTION ALSO IN DRAW FUNCTION WHEN IN EARTHQUAKE MODE- LOOK ABOVE!
    }
    println(theEvent.value());
  }
  
   
  
  
  switch( theEvent.controller().id() ) {
      
    case 201:
      allEarthquakeMode = true;
      bigQuakeMode = false;
      cityMode = false;
      citySel = false;
  
      //map.setCenterZoom(new Location(0, 0), 2);   
      //retargetLocation();  
      toggleButton(buttonAll, true);
  //toggleButton(buttonMajor, false);
  toggleButton(buttonCities, false);
      break;
      
    case 202:
//      allEarthquakeMode = false;
//      bigQuakeMode = true;
//      cityMode = false;
//      println("yougethere");
//      
//      toggleButton(buttonAll, false);
//  //toggleButton(buttonMajor, true);
//  toggleButton(buttonCities, false);
//      break;
     
    
    case 203:
      allEarthquakeMode = false;
      bigQuakeMode = false;
      cityMode = true;
      citySel = false;
      println("pressed city");    
      toggleButton(buttonAll, false);
   //   toggleButton(buttonMajor, false);
      toggleButton(buttonCities, true);
      break;
      
      case 101:
      plotMode = magMode;
      dataParser.setToMagMode();
      
      toggleButton(buttonMag, true);
  toggleButton(buttonDepth, false);
  toggleButton(buttonTime, false);
      break;
      
      case 102:
      plotMode = depthMode;
      dataParser.setToDepthMode();
      
      toggleButton(buttonMag, false);
  toggleButton(buttonDepth, true);
  toggleButton(buttonTime, false);
      break;
      
      case 103:
      plotMode = timeMode;
      dataParser.setToTimeMode();
      
      toggleButton(buttonMag, false);
  toggleButton(buttonDepth, false);
  toggleButton(buttonTime, true);
      break;
      
      case 301:
      
      map.setMapProvider(new Microsoft.HybridProvider());
      
      toggleButton(buttonHybrid, true);
      toggleButton(buttonAerial, false);
      toggleButton(buttonRoad, false);
      break;
      
      case 302:
      
      map.setMapProvider(new Microsoft.AerialProvider());
      toggleButton(buttonHybrid, false);
      toggleButton(buttonAerial, true);
      toggleButton(buttonRoad, false);
      break;
      
      case 303:
      
      map.setMapProvider(new Microsoft.RoadProvider());
      toggleButton(buttonHybrid, false);
      toggleButton(buttonAerial, false);
      toggleButton(buttonRoad, true);
      break;
   
    default:
     println("deafault settings");
      break;
  }
  //switchProvider
 
}

//functions for drawing powerchart

void drawStaticgraph(float plotX1, float plotY1, float plotX2, float plotY2, boolean hidePowerChart){
   
   
   
   if(!hidePowerChart)
   {
   //drawing background  
   rectMode(CORNERS);
   //println(counter);  
   noStroke();
   fill(100, 100, 100, 125);
   rect( plotX1, plotY1, plotX2, plotY2);
    
   //drawing the  grid
    stroke(255); 
    line(plotX1+40,plotY1+40,plotX1+40,plotY1+250);
    
    line(plotX1+40,plotY1+250,plotX2-40,plotY1+250);
    line(plotX2-40,plotY1+40,plotX2-40,plotY1+250);
    //drawing axis labels
    fill(255);
    textSize(12);
    textAlign(CENTER);
    text("Energy (Power of 2)\n 1 unit =15 tons of TNT",plotX1+60,plotY1+15);
    text("Frequency \n(Power of 10)",plotX2-50,plotY1+15);
    text("Fault Dimension (Km)", (plotX1+plotX2)/2, plotY1+265);
    
    // drawing for magnitudes
    for(float i = 1; i < 6; i++)
      {
        stroke(150);
        line(plotX1+40,plotY1+20+(i*40),plotX2-40,plotY1+20+(i*40));
        rectMode(CENTER);
        noStroke();    
  
 /* magnitude colors
 fill(255,230,165); almost white - cream
 fill(254,204,92);
 fill(253,141,60);
  fill(240,59,32);
 fill(189,0,38); - not used
  fill(137,0,0);  dark red almost maroon
      */
            if(i==1)
            {
              fill(255);
              text("200",(plotX1+plotX2)/2,plotY1+5+(i*40));
              fill(0xFFCB181D);
              //fill(255,0,0);
            }
            if(i==2)
            {
              fill(255);
              text("30",(plotX1+plotX2)/2,plotY1+5+(i*40));
              fill(0xFFEF3B2C);
             // fill(255,102,0);
            }
            if(i==3)
            {
              fill(255);
              text("10",(plotX1+plotX2)/2,plotY1+5+(i*40));
               fill(0xFFFB6A4A);
              //fill(255,255,0);
            }
            if(i==4)
            {
              fill(255);
              text("3",(plotX1+plotX2)/2,plotY1+5+(i*40));
             fill(0xFFFC9272);
              //fill(140,255,0);
            }
            if(i==5)
            {
              fill(255);
              text("1",(plotX1+plotX2)/2,plotY1+5+(i*40));
              fill(0xFFFCBBA1);
              //fill(0,255,0);
            }
            rect((plotX1+plotX2)/2,plotY1+20+(i*40),10,20);
         if((mouseX>((plotX1+plotX2)/2)-5) && (mouseX<((plotX1+plotX2)/2)+5))
        
          {
            if(mouseY>(plotY1+20+(i*40))-10 && mouseY<(plotY1+20+(i*40))+10)
              {
                if(i==1)
                {
                  //while(j<(plotX2-plotX1-80+1))
                  //scale(j);
                  rect((plotX1+plotX2)/2,plotY1+20+(i*40),animator*32,20);
                  fill(255);
                  textAlign(LEFT);
                  text("    (Cook County)",(plotX1+plotX2)/2,plotY1+5+(i*40));
                  //plotX2-plotX1-80
                }
                if(i==2)
                {
                  
                  rect((plotX1+plotX2)/2,plotY1+20+(i*40),animator*12,20);//plotX2-plotX1-280
                  fill(255);
                  textAlign(LEFT);
                  text("    (Chicago)",(plotX1+plotX2)/2,plotY1+5+(i*40));
                }
                if(i==3)
                {
                  
                  rect((plotX1+plotX2)/2,plotY1+20+(i*40),animator*5,20);//plotX2-plotX1-350
                  fill(255);
                  textAlign(LEFT);
                  text("    ",(plotX1+plotX2)/2,plotY1+5+(i*40));
                }
                if(i==4)
                {
                  rect((plotX1+plotX2)/2,plotY1+20+(i*40),animator*2,20);//plotX2-plotX1-380
                  fill(255);
                  textAlign(LEFT);
                  text("    (Little Italy)",(plotX1+plotX2)/2,plotY1+5+(i*40));
                    
                }
                if(i==5)
                {
                  rect((plotX1+plotX2)/2,plotY1+20+(i*40),animator,20);//plotX2-plotX1-390
                  fill(255);
                  textAlign(LEFT);
                  text("    (UIC)",(plotX1+plotX2)/2,plotY1+5+(i*40));
                  
                }
              }        
          }
         
            
          
        // frequency and energy axis ticks value labels
        fill(255,255,255);
        textAlign(CENTER);
        text(int(5-i)*5,plotX1+30,plotY1+20+(i*40));
        text(int(i-1),plotX2-30,plotY1+20+(i*40));
        
        
      }
   }
//   else
//   {
//     noStroke();
//   fill(200, 200, 200, 0);
//   rect( plotX1+500, plotY1, plotX2+500, plotY2);
//   }
   
}
void hiddenStaticgraph(float plotX1, float plotY1, float plotX2, float plotY2){
 
  rectMode(CORNERS); 
  noStroke();
  fill(100, 100, 100, 125);
   rect( plotX1, plotY1, plotX2, plotY2);
  
   fill(255);
   text("C\nO\nM\nP\nA\nR\nE", plotX1+15, plotY1+90);
   
   rectMode(CORNER);
}

//TO DO ***************************************************************************
void drawQuakeInfo(int bigQuakeNo, float utcHour, float day, float month, float year, float magnitude, float depth){
  int casualties = casualtyArray[ bigQuakeNo-1];
  
  
  rectMode(CORNERS);
  fill(100, 100, 100, 125);
  rect( 10, windowHeight-950, 100, windowHeight-850);
  textAlign(LEFT);
  textSize(10);
  //fill(255,255,0);
  //stroke(255);
  fill(255);
  text("Quake Info:\nMagnitude = " +magnitude+"\n Depth = "+depth+"\n MM:DD = "+int(month)+":"+int(day)+"\nDeaths = "+casualties, 15, windowHeight - 930);   /////ADD ADDITIONAL LABELS
  
  rectMode(CORNER);
}

void retargetLocation( ){
//HERE I DEAL WITH ZOOM AND PAN- change max and min lat and lon
  if( allEarthquakeMode || cityMode){
    boolean locChanged = false;
    double sc = map.sc;
    Location uL = map.pointLocation(0, 0);  
    if( sc > 4.0 && (upperLeft.lat != uL.lat || upperLeft.lon != uL.lon) ){ 
      if (uL.lat > -90 && uL.lat < 90 && uL.lon > -180 && uL.lon < 180){
        println("upper left should become: lat =  " + uL.lat + " lon = " + uL.lon); 
        upperLeft = new Location( uL.lat, uL.lon);
        locChanged = true;
      } 
    }  
  
    Location uR = map.pointLocation(width, 0);
    if( sc > 4.0 && (upperRight.lat != uR.lat || upperRight.lon != uR.lon) ){
      if (uR.lat > -90 && uR.lat < 90 && uR.lon > -180 && uR.lon < 180){
        upperRight = new Location( uR.lat, uR.lon);
        locChanged = true;
      }
    }
  
    Location lL = map.pointLocation(0, height);
    if( sc > 4.0 && (lowerLeft.lat != lL.lat || lowerLeft.lon != lL.lon) ){
      if (lL.lat > -90 && lL.lat < 90 && lL.lon > -180 && lL.lon < 180){
        lowerLeft = new Location( lL.lat, lL.lon);
        locChanged = true;
      }
    }
  
     Location lR = map.pointLocation(width, height);
     if(  sc > 4.0 && (lowerRight.lat != lR.lat || lowerRight.lon != lR.lon) ){
       if (lR.lat > -90 && lR.lat < 90 && lR.lon > -180 && lR.lon < 180){
        lowerRight = new Location( lR.lat, lR.lon);
        locChanged = true;
       }
     }
   
     if(locChanged ){
        dataParser.reSetLatAndLonRange(lowerLeft.lat, upperLeft.lat, upperLeft.lon, lowerRight.lon, map.sc);
        prevZoomLevel = map.sc;
      } 
  }
} 



//--------------------------------


